home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 23 web forms and controls / databinding / datagridform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  9.6 KB  |  223 lines

  1. Imports System.Data.OleDb
  2.  
  3. Public Class DataGridForm
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected WithEvents dgrTitles As System.Web.UI.WebControls.DataGrid
  7.  
  8. #Region " Web Form Designer Generated Code "
  9.  
  10.     'This call is required by the Web Form Designer.
  11.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  12.  
  13.     End Sub
  14.  
  15.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  16.         'CODEGEN: This method call is required by the Web Form Designer
  17.         'Do not modify it using the code editor.
  18.         InitializeComponent()
  19.     End Sub
  20.     Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
  21.     Protected WithEvents lblTotal As System.Web.UI.WebControls.Label
  22.     Protected WithEvents btnEval As System.Web.UI.WebControls.Button
  23.  
  24. #End Region
  25.  
  26.     ' The DataSet that contains the data.
  27.     Dim ds As New DataSet()
  28.  
  29.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  30.         If Not Me.IsPostBack Then
  31.             ' If this is the first time the form is displayed, read the DataSet
  32.             ' and store it in a Session variable.
  33.             FillDataSet()
  34.             Session("DataSet") = ds
  35.             ' Bind the DataGrid control.
  36.             BindDataGrid()
  37.         Else
  38.             ' If this is a postback, just retrieve the DataSet from the Session variable.
  39.             ds = DirectCast(Session("DataSet"), DataSet)
  40.         End If
  41.     End Sub
  42.  
  43.     ' Fill the DataSet from the Pubs database.
  44.     Sub FillDataSet()
  45.         Dim cn As New OleDbConnection(OledbPubsConnString)
  46.         cn.Open()
  47.         ' Fill the Titles DataTable.
  48.         Dim daTitles As New OleDbDataAdapter("SELECT * FROM Titles", cn)
  49.         daTitles.Fill(ds, "Titles")
  50.  
  51.         ' Fill the Publishers DataTable.
  52.         Dim daPubs As New OleDbDataAdapter("SELECT pub_id,pub_name FROM Publishers", cn)
  53.         daPubs.Fill(ds, "Publishers")
  54.         ' Close the connection
  55.         cn.Close()
  56.         ' Create the relation between the two tables.
  57.         ds.Relations.Add("PubsTitles", ds.Tables("Publishers").Columns("pub_id"), ds.Tables("Titles").Columns("pub_id"))
  58.     End Sub
  59.  
  60.     ' Bind the DataGrid control.
  61.     Sub BindDataGrid()
  62.         ' Bind the data source to the DataGrid.
  63.         dgrTitles.DataSource = ds.Tables("Titles")
  64.         dgrTitles.DataKeyField = "title_id"
  65.  
  66.         ' Retrieve the SortExpression. This value is stored into the 
  67.         ' SortExpr attribute, so that its initial value can be edited
  68.         ' directly in the HTML view 
  69.         Dim sortExpr As String = dgrTitles.Attributes("SortExpr")
  70.  
  71.         ' if the grid must be sorted, then we need to bind to a DataView
  72.         ' instead of directly to the DataGrid
  73.  
  74.         If Not (sortExpr Is Nothing) AndAlso sortExpr.ToString.Length > 0 Then
  75.             ' We must bind to a sorted DataView object instead.
  76.             Dim dv As DataView = ds.Tables("Titles").DefaultView
  77.             dv.Sort = sortExpr.ToString
  78.             dgrTitles.DataSource = dv
  79.         End If
  80.  
  81.         ' Do the binding.
  82.         dgrTitles.DataBind()
  83.  
  84.         ' If we are in edit mode, also bind the ddlPublishers control.
  85.         If dgrTitles.EditItemIndex >= 0 Then
  86.             ' Get a reference to the ddlPublishers control.
  87.             Dim dgi As DataGridItem = dgrTitles.Items(dgrTitles.EditItemIndex)
  88.             Dim ddlPublishers As DropDownList = DirectCast(dgi.FindControl("ddlPublishers"), DropDownList)
  89.  
  90.             ' Bind it to the Publishers DataTable.
  91.             ddlPublishers.DataSource = ds.Tables("Publishers")
  92.             ddlPublishers.DataTextField = "pub_name"
  93.             ddlPublishers.DataValueField = "pub_id"
  94.             ddlPublishers.DataBind()
  95.  
  96.             ' Highlight the publisher of the current title
  97.             Dim dr As DataRow = GetDataRow(dgrTitles.DataKeys(dgrTitles.EditItemIndex).ToString)
  98.             SelectItemFromValue(ddlPublishers, dr("pub_id").ToString)
  99.         End If
  100.  
  101.     End Sub
  102.  
  103.     ' this event fires when the Edit button is clicked 
  104.  
  105.     Private Sub dgrTitles_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.EditCommand
  106.         ' Enter the edit mode and rebind the control.
  107.         dgrTitles.EditItemIndex = e.Item.ItemIndex
  108.         BindDataGrid()
  109.     End Sub
  110.  
  111.     ' this event handler fires when the Cancel button is clicked
  112.  
  113.     Private Sub dgrTitles_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.CancelCommand
  114.         ' Exit edit mode.
  115.         dgrTitles.EditItemIndex = -1
  116.         BindDataGrid()
  117.     End Sub
  118.  
  119.     ' this event handler fires when the Delete button is clicked
  120.  
  121.     Private Sub dgrTitles_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.DeleteCommand
  122.         ' Get a reference to the DataGridItem being deleted.
  123.         Dim dgi As DataGridItem = dgrTitles.Items(e.Item.ItemIndex)
  124.         ' Get the DataRow with the corresponding key value.
  125.         Dim dr As DataRow = GetDataRow(dgrTitles.DataKeys(e.Item.ItemIndex).ToString)
  126.         ' delete it and rebind the control.
  127.         dr.Delete()
  128.         BindDataGrid()
  129.     End Sub
  130.  
  131.     ' this event handler fires when the Update button is clicked
  132.  
  133.     Private Sub dgrTitles_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrTitles.UpdateCommand
  134.         ' Get a reference to the DataGridItem being edited.
  135.         Dim dgi As DataGridItem = dgrTitles.Items(e.Item.ItemIndex)
  136.         ' Get the DataRow with the corresponding key value.
  137.         Dim dr As DataRow = GetDataRow(dgrTitles.DataKeys(e.Item.ItemIndex).ToString)
  138.  
  139.         ' Update DataRow columns. To do that, we must read data
  140.         ' stored in the fields contained in the template
  141.  
  142.         ' The title field comes from the first (only) control in the second cell in this row.
  143.         dr("title") = DirectCast(dgi.Cells(1).Controls(0), TextBox).Text
  144.         ' The pub_id field comes from the current value of the ddlPublishers control.
  145.         Dim ddlPublishers As DropDownList = DirectCast(dgi.FindControl("ddlPublishers"), DropDownList)
  146.         dr("pub_id") = ddlPublishers.SelectedItem.Value
  147.         ' The type field comes from the first (only) control in the fourth cell in this row.
  148.         dr("type") = DirectCast(dgi.Cells(3).Controls(0), TextBox).Text
  149.         ' The price field comes from the first (only) control in the fifth cell in this row.
  150.         dr("price") = CDec(DirectCast(dgi.Cells(4).Controls(0), TextBox).Text)
  151.  
  152.         ' Exit edit mode and rebind the DataGrid.
  153.         dgrTitles.EditItemIndex = -1
  154.         BindDataGrid()
  155.     End Sub
  156.  
  157.     ' This auxiliary routine returns the DataRow with a given key value.
  158.  
  159.     Function GetDataRow(ByVal id As String) As DataRow
  160.         ' Select the DataTable rows with this key value.
  161.         Dim drows() As DataRow = ds.Tables("Titles").Select("title_id='" & id & "'")
  162.         ' Return the DataRow if found.
  163.         If drows.Length > 0 Then
  164.             Return drows(0)
  165.         End If
  166.     End Function
  167.  
  168.     ' this routine shows how you can work with all the "selected" rows
  169.     ' (a selected row is a row whose first column contains a selected checkbox)
  170.  
  171.     Private Sub btnEval_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEval.Click
  172.         ' Purchase all titles that have a selected checkbox.
  173.         Dim totalPrice As Decimal
  174.         Dim count As Integer
  175.         Dim dgi As DataGridItem
  176.  
  177.         For Each dgi In dgrTitles.Items
  178.             ' Get a reference to the CheckBox control in this item.
  179.             Dim cb As CheckBox = DirectCast(dgi.FindControl("chkSelect"), CheckBox)
  180.             ' If checked, delete this element.
  181.             If cb.Checked Then
  182.                 ' Get the title_id key value for this row.
  183.                 Dim id As String = dgrTitles.DataKeys(dgi.ItemIndex).ToString
  184.                 ' Select the DataTable row with this key value.
  185.                 Dim dr As DataRow = GetDataRow(id)
  186.                 If Not dr.IsNull("price") Then
  187.                     ' Add the price to the running total.
  188.                     totalPrice += CDec(dr("price"))
  189.                     count += 1
  190.                 End If
  191.             End If
  192.         Next
  193.  
  194.         ' Display the total
  195.         lblTotal.Text = String.Format("Total price for {0} selected book(s) is ${1}", count, totalPrice)
  196.     End Sub
  197.  
  198.     ' this event handler fires when the Update button is clicked
  199.  
  200.     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  201.         ' Update the Pubs database.
  202.         ' (this demo doesn't do anything)
  203.     End Sub
  204.  
  205.     ' this event handler fires when a hyperlink in a column header is clicked
  206.  
  207.     Private Sub dgrTitles_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgrTitles.SortCommand
  208.         ' Extract current sort column and order.
  209.         Dim currSortExpr As String = dgrTitles.Attributes("SortExpr")
  210.         Dim newSortExpr As String = e.SortExpression
  211.  
  212.         ' If the sort expression is the same as the current one, just reverse the direction.
  213.         If Not (currSortExpr Is Nothing) AndAlso currSortExpr.ToString = e.SortExpression Then
  214.             newSortExpr &= " DESC"
  215.         End If
  216.  
  217.         ' Remember the new sort expression and rebind.
  218.         dgrTitles.Attributes("SortExpr") = newSortExpr
  219.         BindDataGrid()
  220.     End Sub
  221.  
  222. End Class
  223.